package com.edgclub.edg.api;

import android.content.Context;
import android.text.TextUtils;

import com.edgclub.edg.api.param.PostsParam;
import com.edgclub.edg.api.param.TopicReportParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.http.async.AsyncHttpClient;
import com.edgclub.edg.http.async.AsyncHttpResponseHandler;
import com.edgclub.edg.http.async.RequestParams;
import com.edgclub.edg.utils.util.CryptUtil;

import java.io.InputStream;
import java.util.ArrayList;

/**
 * 贴子操作的api
 *
 * @author 陈坤彬
 */
public class TopicApi extends GuZhiApi {

    // 用来管理所有的请求的线程
    private ArrayList<String> tags = new ArrayList<String>();

    private AsyncHttpClient asyncHttpClient;


    public TopicApi(Context mContext) {
        super(mContext);
        asyncHttpClient = new AsyncHttpClient();
    }

    /**
     * 发布贴子
     *
     * @param params
     * @param listener
     */
    public void create(PostsParam params, VolleyListener listener) {
        postOnce(ApiConfig.APIURL.URL_POSTS_CREATE,
                ApiConfig.APIURL.URL_POSTS_CREATE, params, listener);

    }

    /**
     * 修改贴子
     *
     * @param params
     * @param listener
     */
    public void update(PostsParam params, VolleyListener listener) {
        postOnce(ApiConfig.APIURL.URL_POSTS_UPDATE,
                ApiConfig.APIURL.URL_POSTS_UPDATE, params, listener);

    }

    /**
     * 修改贴子 --删除图片
     *
     * @param params
     * @param listener
     */
    public void deleteImg(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_UPDATE_DELETE_IMG,
                ApiConfig.APIURL.URL_POSTS_UPDATE_DELETE_IMG, params, listener);

    }

    /**
     * 修改贴子 --修改图片顺序
     *
     * @param params
     * @param listener
     */
    public void updateSeq(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_UPDATE_SEQ,
                ApiConfig.APIURL.URL_POSTS_UPDATE_SEQ, params, listener);

    }



    /**
     * 获取EDG的贴子
     *
     * @param params
     * @param listener
     */
    public void getEdgPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_EDG,
                ApiConfig.APIURL.URL_POSTS_EDG, params, listener);
    }

    /**
     * 获取关注列表
     *
     * @param params
     * @param listener
     */
    public void attentionPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_FRIEND, ApiConfig.APIURL.URL_POSTS_FRIEND,
                params, listener);
        tags.add(ApiConfig.APIURL.URL_POSTS_FRIEND);
    }

    /**
     * 获取活动列表
     *
     * @param params
     * @param listener
     */
    public void getEdgActivity(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_EDG_ACTIVITY, ApiConfig.APIURL.URL_EDG_ACTIVITY,
                params, listener);
    }

    /**
     * 获取队员列表
     *
     * @param params
     * @param listener
     */
    public void getEdgTeam(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_EDG_TEAM, ApiConfig.APIURL.URL_EDG_TEAM,
                params, listener);
    }

    /**
     * 获取全部帖子
     *
     * @param params
     * @param listener
     */
    public void getbbPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_BB, ApiConfig.APIURL.URL_POSTS_BB,
                params, listener);
        tags.add(ApiConfig.APIURL.URL_POSTS_BB);
    }

    /**
     * 获取帖子频道列表
     *
     * @param params
     * @param listener
     */
    public void channelPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_CHANNEL, ApiConfig.APIURL.URL_POSTS_CHANNEL,
                params, listener);
        tags.add(ApiConfig.APIURL.URL_POSTS_CHANNEL);
    }

    /**
     * 获取banner列表
     *
     * @param params
     * @param listener
     */
    public void getBannerList(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_BANNER_LIST, ApiConfig.APIURL.URL_POSTS_BANNER_LIST,
                params, listener);
    }
    /**
     * 查看个人帖子接口
     *
     * @param params
     * @param listener
     */
    public void getUserPosts(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_USER_POSTS, ApiConfig.APIURL.URL_POSTS_USER_POSTS,
                params, listener);
        tags.add(ApiConfig.APIURL.URL_POSTS_USER_POSTS);
    }
    /**
     * 获取热帖
     *
     * @param params
     * @param listener
     */
    public void channelPost(PostsParam params, String url, VolleyListener listener) {
        postByUrl(ApiConfig.APIURL.URL_POSTS_CHANNEL, url,
                params, listener);
    }

    /**
     * 获取大咖
     *
     * @param params
     * @param listener
     */
//    public void dakaPost(PostsParam params, VolleyListener listener) {
//        post(ApiConfig.APITAG.TAG_POSTS_DAKA, ApiConfig.APIURL.URL_POSTS_DAKA,
//                params, listener);
//        tags.add(ApiConfig.APITAG.TAG_POSTS_DAKA);
//    }

    /**
     * 获取热贴
     *
     * @param params
     * @param listener
     */
//    public void friendPost(PostsParam params, VolleyListener listener) {
//        post(ApiConfig.APITAG.TAG_POSTS_FRIEND, ApiConfig.APIURL.URL_POSTS_FRIEND,
//                params, listener);
//        tags.add(ApiConfig.APITAG.TAG_POSTS_FRIEND);
//    }

    /**
     * 获取回复我的贴子
     *
     * @param params
     * @param listener
     */
    public void replyMyPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_MESS_ATME , ApiConfig.APIURL.URL_MESS_ATME,
                params, listener);
        tags.add(ApiConfig.APIURL.URL_MESS_ATME);
    }

    /**
     * 获取点赞列表
     *
     * @param params
     * @param listener
     */
//    public void likeMePost(PostsAtMeParam params, VolleyListener listener) {
//        post(ApiConfig.APITAG.TAG_POSTS_LIKEME, ApiConfig.APIURL.URL_POSTS_LIKEME,
//                params, listener);
//        tags.add(ApiConfig.APITAG.TAG_POSTS_LIKEME);
//    }

    /**
     * 贴子收藏
     *
     * @param params
     * @param listener
     */
    public void collectionPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_COLLECTION,
                ApiConfig.APIURL.URL_POSTS_COLLECTION, params, listener);
    }

    /**
     * 取消贴子收藏
     *
     * @param params
     * @param listener
     */
    public void cancleCollectionPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_CANCLE_COLLECTION,
                ApiConfig.APIURL.URL_POSTS_CANCLE_COLLECTION, params, listener);
    }

    /**
     * 获取我的收藏
     *
     * @param params
     * @param listener
     */
    public void myCollection(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_MY_COLLECTION, ApiConfig.APIURL.URL_POSTS_MY_COLLECTION,
                params, listener);
    }

    /**
     * 获取点赞成员列表
     *
     * @param params
     * @param listener
     */
//    public void getpraiseList(PostsAtMeParam params, VolleyListener listener) {
//        post(ApiConfig.APITAG.TAG_POSTS_PRAISE_LIST, ApiConfig.APIURL.URL_POSTS_PRAISE_LIST,
//                params, listener);
//        tags.add(ApiConfig.APITAG.TAG_POSTS_PRAISE_LIST);
//    }

    /**
     * 获取banner列表
     *
     * @param params
     * @param listener
     */
//    public void getBannerList(PostsNearbyParam params, VolleyListener listener) {
//        post(ApiConfig.APITAG.TAG_POSTS_BANNER_LIST, ApiConfig.APIURL.URL_POSTS_BANNER_LIST,
//                params, listener);
//    }

    /**
     * 贴子点赞
     *
     * @param params
     * @param listener
     */
    public void likePost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_LIKE,
                ApiConfig.APIURL.URL_POSTS_LIKE, params, listener);
    }

    /**
     * 贴子取消点赞
     *
     * @param params
     * @param listener
     */
    public void unlikePost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_UNLIKE,
                ApiConfig.APIURL.URL_POSTS_UNLIKE, params, listener);
    }
    /**
     * 评论点赞
     *
     * @param params
     * @param listener
     */
    public void likereplyPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REPLY_POSTS_LIKE,
                ApiConfig.APIURL.URL_REPLY_POSTS_LIKE, params, listener);
    }

    /**
     * 评论取消点赞
     *
     * @param params
     * @param listener
     */
    public void unlikereplyPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REPLY_POSTS_UNLIKE,
                ApiConfig.APIURL.URL_REPLY_POSTS_UNLIKE, params, listener);
    }

    /**
     * 贴子评论点赞
     *
     * @param params
     * @param listener
     */
//    public void replylikePost(PostsReplyCommentParam params, VolleyListener listener) {
//        postOnce(ApiConfig.APITAG.TAG_POSTS_REPLY_LIKE,
//                ApiConfig.APIURL.URL_POSTS_REPLY_LIKE, params, listener);
//    }

    /**
     * 贴子评论取消点赞
     *
     * @param params
     * @param listener
     */
//    public void replyunlikePost(PostsReplyCommentParam params, VolleyListener listener) {
//        postOnce(ApiConfig.APITAG.TAG_POSTS_REPLY_UNLIKE,
//                ApiConfig.APIURL.URL_POSTS_REPLY_UNLIKE, params, listener);
//    }


    /**
     * 获取贴子详情
     *
     */
    public void detailPost(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_DETAIL,
                ApiConfig.APIURL.URL_POSTS_DETAIL, params, listener);
        tags.add(ApiConfig.APIURL.URL_POSTS_DETAIL);
    }


    /**
     * 回复帖子
     *
     */
    public void replyTopic(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_REPLYTOPIC,
                ApiConfig.APIURL.URL_POSTS_REPLYTOPIC, params, listener);
    }

    /**
     * 回复评论
     *
     */
    public void replyComment(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_REPLYCOMMNET,
                ApiConfig.APIURL.URL_POSTS_REPLYCOMMNET, params, listener);
    }

    /**
     * 获取更多回复
     *
     */
    public void moreReply(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_MOREREPLY,
                ApiConfig.APIURL.URL_POSTS_MOREREPLY, params, listener);
        tags.add(ApiConfig.APIURL.URL_POSTS_MOREREPLY);
    }



    /**
     * 帖子加精
     *
     */
    public void addToDigest(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_ADD_TO_DIGEST,
                ApiConfig.APIURL.URL_POSTS_ADD_TO_DIGEST, params, listener);
    }

    /**
     * 删除帖子
     *
     */
    public void deleteTopic(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_DELETE,
                ApiConfig.APIURL.URL_POSTS_DELETE, params, listener);
    }

    /**
     * 删除回复
     *
     */
    public void deleteReply(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_DELREPLY,
                ApiConfig.APIURL.URL_POSTS_DELREPLY, params, listener);
    }

    /**
     * 获取金币数量
     *
     */
    public void getGoldNum(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_GET_GOLD_NUM,
                ApiConfig.APIURL.URL_POSTS_GET_GOLD_NUM, params, listener);
    }

    /**
     * 赏金币
     *
     */
    public void gold(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_GOLD,
                ApiConfig.APIURL.URL_POSTS_GOLD, params, listener);
    }

    /**
     * 告诉后台贴子图片上传成功
     *
     * @param params
     * @param listener
     */
    public void imageFinish(PostsParam params, VolleyListener listener) {
        postOnce(ApiConfig.APIURL.URL_POSTS_FINISH,
                ApiConfig.APIURL.URL_POSTS_FINISH, params, listener);
    }

    /**
     * 告诉后台回复图片上传成功
     *
     * @param params
     * @param listener
     */
    public void replyimageFinish(PostsParam params, VolleyListener listener) {
        postOnce(ApiConfig.APIURL.URL_POSTS_REPLY_FINISH,
                ApiConfig.APIURL.URL_POSTS_REPLY_FINISH, params, listener);
    }
    /**
     * 获取点赞列表
     *
     */
    public void getlikeMembers(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_POSTS_LIKE_LIST,
                ApiConfig.APIURL.URL_POSTS_LIKE_LIST, params, listener);
    }
    /**
     * 检测帖子是否被举报
     *
     * @param params
     * @param listener
     */
    public void checkReport(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_CHECKTOPICREPORT,
                ApiConfig.APIURL.URL_CHECKTOPICREPORT, params, listener);
        tags.add(ApiConfig.APIURL.URL_CHECKTOPICREPORT);
    }
    /**
     * 帖子举报结果关注
     *
     * @param params
     * @param listener
     */
    public void reportAttention(PostsParam params,
                                VolleyListener listener) {
        postOnce(ApiConfig.APIURL.URL_TOPICREPORTATTENTION,
                ApiConfig.APIURL.URL_TOPICREPORTATTENTION, params, listener);
    }
    /**
     * 视频主页
     *
     * @param params
     * @param listener
     */
    public void edgvideo(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_VIDEO,
                ApiConfig.APIURL.URL_VIDEO, params, listener);
        tags.add(ApiConfig.APIURL.URL_VIDEO);
    }
//    /**
//     * 贴子回复
//     *
//     * @param params
//     * @param listener
//     */
//    public void replyMyPost(PostsParam params, VolleyListener listener) {
//        post(ApiConfig.APIURL.URL_REPLY,
//                ApiConfig.APIURL.URL_REPLY, params, listener);
//        tags.add(ApiConfig.APIURL.URL_REPLY);
//    }

    /**
     * 回复举报
     *
     * @param params
     * @param listener
     */
    public void replyReport(TopicReportParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REPLYREPORT,
                ApiConfig.APIURL.URL_REPLYREPORT, params, listener);
    }

    /**
     * 帖子举报
     *
     * @param params
     * @param listener
     */
    public void topicReport(TopicReportParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_TOPICREPORT,
                ApiConfig.APIURL.URL_TOPICREPORT, params, listener);
    }
    /**
     * 帖子分享接口
     *
     * @param params
     * @param listener
     */
    public void shareTopic(TopicReportParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_TOPICSHARE,
                ApiConfig.APIURL.URL_TOPICSHARE, params, listener);
    }
    /**
     * 帖子打赏榜单详情接口
     *
     * @param params
     * @param listener
     */
    public void rewardList(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REWARE_LIST,
                ApiConfig.APIURL.URL_REWARE_LIST, params, listener);
    }



    /**
     * 回复举报
     *
     * @param param
     * @param asyncHttpResponseHandler
     */
    public void replyReport(InputStream instream, TopicReportParam param,
                            AsyncHttpResponseHandler asyncHttpResponseHandler) {

        RequestParams params = new RequestParams();

        if (null != instream) {
            params.put("request", instream);
        }

        if (null != param.getCategory()
                && !TextUtils.isEmpty(param.getCategory())) {
            params.put("category", param.getCategory());
        }

        if (null != param.getContent()
                && !TextUtils.isEmpty(param.getContent())) {
            params.put("content", param.getContent());
        }

        if (null != param.getPostsId()
                && !TextUtils.isEmpty(param.getPostsId())) {
            params.put("postsId", param.getPostsId());
        }


        if (null != param.getPostUserId()
                && !TextUtils.isEmpty(param.getPostUserId())) {
            params.put("postUserId", param.getPostUserId());
        }

        if (null != param.getUserId()
                && !TextUtils.isEmpty(param.getUserId())) {
            params.put("ReportUserId", param.getUserId());
        }

        if (null != param.getReplyId()
                && !TextUtils.isEmpty(param.getReplyId())) {
            params.put("replyId", param.getReplyId());
        }

        String t = System.currentTimeMillis() + "";
        params.put("t", t);

        if (null == param.getContent() || TextUtils.isEmpty(param.getContent())) {
            String temp = "";
            if (null == param.getReplyId() || TextUtils.isEmpty(param.getReplyId())) {
                temp = "ReportUserId=" + param.getUserId()
                        + "&category=" + param.getCategory()
                        + "&postUserId=" + param.getPostUserId()
                        + "&postsId=" + param.getPostsId()
                        + "&t=" + t
                        + "&key=" + ApiConfig.HTTP_KEY;
                String temp2 = CryptUtil.md5(temp);

                params.put(
                        "sign",
                        temp2);
                asyncHttpClient.post(ApiConfig.BASE_URL
                                + ApiConfig.APIURL.URL_TOPICREPORT, params,
                        asyncHttpResponseHandler);
            } else {
                temp = "ReportUserId=" + param.getUserId()
                        + "&category=" + param.getCategory()
                        + "&postUserId=" + param.getPostUserId()
                        + "&postsId=" + param.getPostsId()
                        + "&replyId=" + param.getReplyId()
                        + "&t=" + t
                        + "&key=" + ApiConfig.HTTP_KEY;
                String temp2 = CryptUtil.md5(temp);

                params.put(
                        "sign",
                        temp2);
                asyncHttpClient.post(ApiConfig.BASE_URL
                                + ApiConfig.APIURL.URL_REPLYREPORT, params,
                        asyncHttpResponseHandler);
            }

        } else {

            String temp = "";
            if (null == param.getReplyId() || TextUtils.isEmpty(param.getReplyId())) {
                temp = "ReportUserId=" + param.getUserId()
                        + "&category=" + param.getCategory()
                        + "&content=" + param.getContent()
                        + "&postUserId=" + param.getPostUserId()
                        + "&postsId=" + param.getPostsId()
                        + "&t=" + t
                        + "&key=" + ApiConfig.HTTP_KEY;
                String temp2 = CryptUtil.md5(temp);

                params.put(
                        "sign",
                        temp2);
                asyncHttpClient.post(ApiConfig.BASE_URL
                                + ApiConfig.APIURL.URL_TOPICREPORT, params,
                        asyncHttpResponseHandler);
            } else {
                temp = "ReportUserId=" + param.getUserId()
                        + "&category=" + param.getCategory()
                        + "&content=" + param.getContent()
                        + "&postUserId=" + param.getPostUserId()
                        + "&postsId=" + param.getPostsId()
                        + "&replyId=" + param.getReplyId()
                        + "&t=" + t
                        + "&key=" + ApiConfig.HTTP_KEY;
                String temp2 = CryptUtil.md5(temp);

                params.put(
                        "sign",
                        temp2);
                asyncHttpClient.post(ApiConfig.BASE_URL
                                + ApiConfig.APIURL.URL_REPLYREPORT, params,
                        asyncHttpResponseHandler);
            }

        }
    }


    /**
     * 举报记录列表
     *
     * @param params
     * @param listener
     */
    public void reportList(TopicReportParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REPORT_LIST,
                ApiConfig.APIURL.URL_REPORT_LIST, params, listener);
    }

    /**
     * 举报记录更新状态
     *
     * @param params
     * @param listener
     */
    public void updateReport(TopicReportParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REPORT_UPDATE,
                ApiConfig.APIURL.URL_REPORT_UPDATE, params, listener);
    }

    /**
     * 帖子添加到某个频道接口
     *
     * @param params
     * @param listener
     */
    public void addChannel(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_ADD_CHANNEL,
                ApiConfig.APIURL.URL_ADD_CHANNEL, params, listener);
    }

    /**
     *
     * 帖子移出频道接口
     *
     * @param params
     * @param listener
     */
    public void removeChannel(PostsParam params, VolleyListener listener) {
        post(ApiConfig.APIURL.URL_REMOVE_CHANNEL,
                ApiConfig.APIURL.URL_REMOVE_CHANNEL, params, listener);
    }

    public void cancelAll() {
        for (String tag : tags) {
            cancel(tag);
        }
    }

}
